﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public partial class ProductCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var lst = BUS.LoaiSanPhamBUS.GetAll();
            gvLoaiSanPham.DataSource = lst;
            gvLoaiSanPham.DataBind();
        }

        protected void gvLoaiSanPham_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvLoaiSanPham.EditIndex = -1;
            FillData();
        }

        protected void gvLoaiSanPham_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                LoaiSanPham loanSanPham = new LoaiSanPham();
                loanSanPham.MaLoai = Convert.ToString(((TextBox)gvLoaiSanPham.FooterRow.FindControl("txtMaLoai")).Text);
                loanSanPham.TenLoaiSanPham = Convert.ToString(((TextBox)gvLoaiSanPham.FooterRow.FindControl("txtTenLoai")).Text);

                BUS.LoaiSanPhamBUS.Insert(loanSanPham);
                FillData();

            }

        }

        protected void gvLoaiSanPham_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvLoaiSanPham.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gvLoaiSanPham_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(gvLoaiSanPham.DataKeys[e.RowIndex].Values[0].ToString());
            BUS.LoaiSanPhamBUS.Delete(id);
            FillData();
        }

        protected void gvLoaiSanPham_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            LoaiSanPham loaiSanPham = new LoaiSanPham();
            loaiSanPham.Id = Convert.ToInt32(gvLoaiSanPham.DataKeys[e.RowIndex].Values[0].ToString());
            loaiSanPham.MaLoai = Convert.ToString(((TextBox)gvLoaiSanPham.Rows[e.RowIndex].FindControl("txtMaLoai")).Text);
            loaiSanPham.TenLoaiSanPham = Convert.ToString(((TextBox)gvLoaiSanPham.Rows[e.RowIndex].FindControl("txtTenLoai")).Text);

            BUS.LoaiSanPhamBUS.Update(loaiSanPham);
            gvLoaiSanPham.EditIndex = -1;
            FillData();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {

            TextBox txtMaLoai = (sender as Button).NamingContainer.FindControl("txtMaLoai") as TextBox;
            TextBox txtTenLoai = (sender as Button).NamingContainer.FindControl("txtTenLoai") as TextBox;
            LoaiSanPham loaiSanPham = new LoaiSanPham();
            loaiSanPham.TenLoaiSanPham = txtTenLoai.Text;
            loaiSanPham.MaLoai = txtMaLoai.Text;

            BUS.LoaiSanPhamBUS.Insert(loaiSanPham);
            FillData();
        }
    }
}